package com.chinasoft.mall.activity.subActivity.search;

import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.chinasoft.mall.R;
import com.chinasoft.mall.activity.BaseActivity;
import com.chinasoft.mall.activity.MainActivity;
import com.chinasoft.mall.activity.adapter.SearchAdapter;
import com.chinasoft.mall.framework.BeanCache;
import com.chinasoft.mall.framework.Config;
import com.chinasoft.mall.framework.ControllerCenter;
import com.chinasoft.mall.model.BaseBean;
import com.chinasoft.mall.util.SystemUtil;
import com.chinasoft.mall.widget.LoadListView;

public class SearchActivity extends BaseActivity implements OnClickListener{
	
	private BaseBean searchgoods;
	private BaseBean reci_bb;
	private SearchAdapter mlAdapter;
	private ImageButton search_btn;
	private LoadListView hotword;
	private EditText search_et;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.search);
		initView();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		initRequest();
	}

	private void initRequest() {
		reci_bb = BeanCache.getInstance().getResponsdata().get(Config.GET_RECI);
		if (reci_bb != null) {
			success(reci_bb);
		} else {
			reci_bb = new BaseBean(Config.GET_RECI);
			control.sendMessage(reci_bb);
		}
	}
	
	private void initView() {
		search_btn=(ImageButton) findViewById(R.id.search_btn);
		hotword=(LoadListView) findViewById(R.id.hotword);
		search_et=(EditText) findViewById(R.id.search_et);
		mlAdapter=new SearchAdapter((MainActivity)this.getParent(), null);
		hotword.setAdapter(mlAdapter);
		search_btn.setOnClickListener(this);
		search_et.setOnClickListener(this);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void success(BaseBean obj) {
		super.success(obj);
		if(obj.equals(reci_bb)){
			mlAdapter.setData((List<String>)obj.getResData().getData());
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode==KeyEvent.KEYCODE_ENTER){
			searchMethod();
			return true;
		}else{
			return super.onKeyDown(keyCode, event);
		}
	}

	private void searchMethod() {
		String searchcontent=search_et.getText().toString();
		if("".equals(searchcontent)){
			Toast.makeText(this, "请输入搜索内容", Toast.LENGTH_LONG).show();
			return;
		}
		searchgoods=new BaseBean(Config.SEACH_GOODS);
		if(searchcontent.matches("[0-9]?")){
			searchgoods.reqData.put("pub_date", searchcontent);
			((MainActivity)this.getParent()).setCurrentActivity(SearchListActivity.class, searchgoods.reqData.get("goods_name"), searchgoods);
		}else{
			searchgoods.reqData.put("goods_name", searchcontent);
			((MainActivity)this.getParent()).setCurrentActivity(SearchListActivity.class, searchgoods.reqData.get("goods_name"), searchgoods);
		}
		SystemUtil.closeKeyboard(this, search_et);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.search_et:
			InputMethodManager imm = (InputMethodManager)search_et.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);  
			imm.showSoftInput(search_et, InputMethodManager.SHOW_FORCED);
			break;
		case R.id.search_btn:
			searchMethod();
			break;
		default:
			break;
		}
	}
}
